module.exports = {
    translate(root) {
        if (root.indexOf('color:') > -1 || root.indexOf('color :') > -1) {
            return {
                success: false,
                error: '请清除背景或字体颜色'
            };
        }
        //处理行标
        let liOrder = 1;
        while (root.indexOf('<li>') > -1) {
            if (root.indexOf('<ol><li>') > -1) {
                const start = root.indexOf('<ol>');
                if (root.indexOf('<li>') > start) {
                    //新的列表
                    liOrder = 1;
                    console.log('new');
                    root = root.replace('<ol>', '').replace('</ol>', '');
                }
            }
            root = root.replace('<li>', '<p>' + liOrder + '.').replace('</li>', '</p>');
            liOrder++;
        }

        //处理字体
        while (root.indexOf('class=') > -1) {
            const _classprec = root.substring(0, root.indexOf('class=') - 1);
            const clsLabelName = _classprec.substring(_classprec.lastIndexOf('<') + 1, _classprec.length); //所属标签名
            const clsBqIndex = root.indexOf('class') - (clsLabelName.length + 2); //class所在标签头相对root的index
            const _clsLabel2End = root.substring(clsBqIndex, root.length);
            const clsAll = _clsLabel2End.substring(0, _clsLabel2End.indexOf('</' + clsLabelName + '>') + (clsLabelName.length + 3)); //所属全部标签内容
            const clsInner = clsAll.substring(clsAll.indexOf('>') + 1, clsAll.indexOf('</' + clsLabelName + '>')); //标签内部所有内容
            const _fontFamily2clsAllEnd = clsAll.substring(clsAll.indexOf('class="') + 7, clsAll.length);
            const fontFamily = _fontFamily2clsAllEnd.substring(8, _fontFamily2clsAllEnd.indexOf('"')); //字体

            /****after****/
            const dictFontFamily = { SimSun: '宋体', SimHei: '黑体', 'Microsoft-YaHei': '微软雅黑', KaiTi: '楷体', FangSong: '仿宋' };
            const clsInnerDone = '<font face=' + dictFontFamily[fontFamily] + '_GB2312>' + clsInner + '</font>';
            /****replace****/
            root = root.replace(clsInner, clsInnerDone);
            root = root.replace(' class="ql-font-' + fontFamily + '"', '');
        }
        //处理字号
        while (root.indexOf('style=') > -1) {
            const _styleprec = root.substring(0, root.indexOf('style=') - 1);
            const styleLabelName = _styleprec.substring(_styleprec.lastIndexOf('<') + 1, _styleprec.length); //所属标签名
            const styleBqIndex = root.indexOf('style') - (styleLabelName.length + 2); //class所在标签头相对root的index
            const _styleLabel2End = root.substring(styleBqIndex, root.length);
            const styleAll = _styleLabel2End.substring(0, _styleLabel2End.indexOf('</' + styleLabelName + '>') + (styleLabelName.length + 3)); //所属全部标签内容
            const styleInner = styleAll.substring(styleAll.indexOf('>') + 1, styleAll.indexOf('</' + styleLabelName + '>')); //标签内部所有内容
            const _fontSize2clsAllEnd = styleAll.substring(styleAll.indexOf('style="') + 18, styleAll.length);
            const fontSize = _fontSize2clsAllEnd.substring(0, _fontSize2clsAllEnd.indexOf('px')); //字体

            /****after****/
            let styleInnerDone = '';
            const dictSize = { 20: '14.5', 21: '15', 22: '16', 23: '17', 24: '18', 25: '19', 26: '20' };
            if (styleInner.indexOf('<font') > -1) {
                //内部存在font标签,插入属性
                const mark = ' size=' + dictSize[fontSize] + '';
                styleInnerDone = styleInner.substring(0, styleInner.indexOf('<font') + 5) + mark + styleInner.substring(styleInner.indexOf('<font') + 5, styleInner.length);
            } else {
                styleInnerDone = '<font size=' + dictSize[fontSize] + '>' + styleInner + '</font>';
            }
            /****replace****/
            root = root.replace(styleInner, styleInnerDone);
            root = root.replace(' style="font-size: ' + fontSize + 'px;"', '');
        }
        root = root.replaceAll('<em>', '<i>').replaceAll('</em>', '</i>');
        root = root.replaceAll('<strong>', '<b>').replaceAll('</strong>', '</b>');
        root = root.replaceAll('<span>', '').replaceAll('</span>', '');
        return {
            success: true,
            root
        };
    }
}